﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Rison.BusinessFacade.Common;
using Rison.DataAccess.Data.XT;
using Rison.DataAccess.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Entity.XT;

namespace Rison.BusinessFacade.Facade.XT
{
    public sealed class SysUserFacade : FacadeBase
    {
        protected override DataAccessBase dbObject
        {
            get
            {
                if (dbHelper == null)
                    dbHelper = new SysUserDb();

                return dbHelper;
            }
        }

        protected override string identityFieldName
        {
            get { return "UID"; }
        }

        //对密码加密
        protected override List<SqlField> GetSqlFieldCollection(object value, bool getIdentityField)
        {
            List<SqlField> fields = new List<SqlField>();
            if (getIdentityField)
                fields.Add(new SqlField("UID", (value as SysUser).UID, true, false));
            fields.Add(new SqlField("UserId", (value as SysUser).UserId));
            fields.Add(new SqlField("Password", Encryptor.Instance.Encrypt((value as SysUser).Password)));
            fields.Add(new SqlField("Name", (value as SysUser).Name));
            fields.Add(new SqlField("Sex", (value as SysUser).Sex));
            fields.Add(new SqlField("RegisteTime", (value as SysUser).RegisteTime, false));
            fields.Add(new SqlField("Description", (value as SysUser).Description));

            return fields;
        }

        protected override object SetPropertiesValue(DataRow row)
        {
            SysUser obj = new SysUser();
            obj.UID = Convert.ToInt32(row["uid"]);
            obj.UserId = row["UserId"].ToString();
            obj.Password = Encryptor.Instance.Decrypt(row["Password"].ToString());
            obj.Name = StringHelper.GetValue(row, "Name");
            obj.Sex = StringHelper.GetValue(row, "Sex");
            obj.RegisteTime = StringHelper.GetDateTimeValue(row, "RegisteTime");
            obj.Description = StringHelper.GetValue(row, "Description");

            return obj;
        }



    }
}
